package com.loren.demo27.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {

    private int[] intValues;

    private String[] strValues;

    @Override
    public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
        if (object == null) {
            return true;
        }
        if (object instanceof Integer) {
            for (int value : intValues) {
                if (value == (Integer) object) {
                    return true;
                }
            }
        } else if (object instanceof String) {
            for (String value : strValues) {
                if (value.equals(object)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void initialize(EnumValue constraintAnnotation) {
        intValues = constraintAnnotation.intValues();
        strValues = constraintAnnotation.strValues();
    }
}
